package com.xgry.bpm.common.utils;

import lombok.Data;

import java.util.ArrayList;
import java.util.List;

/**
 * @Auther: CN-xin
 * @Date: 2019-10-26 09:32
 * @Description:
 */
@Data
public class MiniUITreeCheckUtil {

    //节点id
    private String id;
    //父节点id
    private String parenIid;
    //节点名称
    private String title;
    //图标
    private String iconCls;
    //url
    private String url;
    //模块子节点集合
    private List<MiniUITreeCheckUtil> modulers;
    //菜单子节点集合
    private List<MiniUITreeCheckUtil> children;
    //按钮子节点集合
    private List<MiniUITreeCheckUtil> functions;

    //是否选中
    private String checked;

    public MiniUITreeCheckUtil() {

    }

    public MiniUITreeCheckUtil(String id, String parenIid, String text, String iconCls, String url, String checked) {
        this.id = id;
        this.parenIid = parenIid;
        this.title = text;
        this.iconCls = iconCls;
        this.url = url;
        this.checked = checked;
    }

    /**
     * 递归（先得到父节点）再递归
     *
     * @param list
     * @return
     */
    public static List<MiniUITreeCheckUtil> getTreeResult(List<MiniUITreeCheckUtil> list) {
        List<MiniUITreeCheckUtil> listResult = new ArrayList<>();
        for (MiniUITreeCheckUtil t : list) {
            if (t.getParenIid().equals("-1" )) {
                listResult.add(t);// 得到父类
            }
        }
        secondTree(list, listResult);
        System.out.println("结果集" + listResult);
        return listResult;
    }

    /**
     * 父节点获取子节点
     *
     * @param list
     * @param listResult
     * @return
     */

    public static void secondTree(List<MiniUITreeCheckUtil> list, List<MiniUITreeCheckUtil> listResult) {
        for (MiniUITreeCheckUtil tResult : listResult) {
            List<MiniUITreeCheckUtil> childrens = new ArrayList<>();
            List<MiniUITreeCheckUtil> functions = new ArrayList<>();
            for (MiniUITreeCheckUtil t : list) {
                if (t.getParenIid().equals("-1" )) {
                    continue;
                }
                //判断id长度为6位的是菜单id ，存入childrens
                if (tResult.getId().equals(t.getParenIid())) {
//                    && t.getId().length() != 9
                    childrens.add(t);
                }
                //判断id长度为9为的是按钮id ，存入functions
                if (tResult.getId().equals(t.getParenIid()) && tResult.getId().length() == 9 && t.getId().length() == 12) {
                    functions.add(t);
                }
            }
            tResult.setChildren(childrens);
            tResult.setFunctions(functions);
            if (!tResult.getChildren().isEmpty()) {
                secondTree(list, tResult.getChildren());
            }
        }

    }

}
